import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Objects;

@Data
@NoArgsConstructor
public class Person implements Comparable {
    private String name;
    private int age;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return age == person.age && Objects.equals(name, person.name);
    }

    public Person(int age) {
        this.age = age;
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    /**
     * 按照年龄从小到大排序
     *
     * @param o the object to be compared.
     */
    @Override
    public int compareTo(Object o) {
        int value;
        if (o instanceof Person person) {
            value = this.age - person.getAge();
            if (value != 0)
                return value;
            return this.name.compareTo(person.getName());
        }
        throw new ClassCastException("不可比较");
    }
}
